package com.laolei.amqp.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Payload;

@SpringBootApplication
public class AmqpConfigApplication implements CommandLineRunner {
  
	final static String queue_name="Test.queue2";
	@Autowired RabbitTemplate rabbitTemplate;
	@Bean Jackson2JsonMessageConverter jackson2JsonMessageConverter(){
		return new Jackson2JsonMessageConverter();
	}
	
	@Bean Queue fooQueue() {
		return new Queue(queue_name);
	}
	
	@Override
	public void run(String... args) throws Exception {
		for (int i = 0; i < 10; i++) 
			rabbitTemplate.convertAndSend(queue_name, new User(i, "张三", "guest")  );
	}
	
	@RabbitListener(queues=queue_name,group="001")@RabbitHandler void subscibe001(@Payload User user) {
		System.err.println("001 ---> "+user);
	}
	
 	@RabbitListener(queues=queue_name,group="002")@RabbitHandler void subscibe002(@Payload User user) {
		System.err.println("002 ---> "+user);
	} 
	
// 	@RabbitListener(queues=queue_name,group="003")
//	ChannelAwareMessageListener messageListener(ConnectionFactory connectionFactory) {
//		return (Message message, Channel channel)->{
//			System.err.println("---------------");
//	        channel.basicQos(100);
//	        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//			System.err.println(message.getMessageProperties());
// 		};
//	}
 	
	public static void main(String[] args) {
		SpringApplication.run(AmqpConfigApplication.class, args);

	}
}
